function get_url_rpath(){
    let url = decodeURIComponent(window.location.pathname)
    // 排除"/list/" -> "xxx/yyy.mkm"
    let path_without_prefix = url.split("/").slice(2).join("/")
    return path_without_prefix
}

function save_as(exportType){
    editor.minder.exportData("json").then(function (content) {
        let msg = {
            data: content,
            path: get_url_rpath(),
            type: exportType
        }
        $.post("/save_as/", msg, function(data, result){
            if (result == "success") {
                alert("[+] 保存成功: " + data["saved"])
            } else {
                alert("[-] 操作失败！")
            }
        })
    })
}

(function() {
    /*$("body").append([
            '<button class="save extends" type_as="mkm">保存mkm</button>',
            '<button class="export extends" type_as="km">保存km</button>',
            '<button class="export extends" type_as="md">导出md</button>',
            '<button class="delete extends">删除</button>',
            '<button class="new extends">新建</button>',
        ])
    */

    $("body").append([
'<button class="extends" id="homepage">主目录</button>',
'<div class="dropdown" id="save_group">\
  <button class="btn btn-default dropdown-toggle extends" type="button" id="save_btns" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">\
    保存与导出\
    <span class="caret"></span>\
  </button>\
  <ul class="dropdown-menu" aria-labelledby="save_btns" style="min-width:109px">\
    <li><a class="save" type_as="mkm">保存mkm</a></li>\
    <li><a class="export" type_as="km">保存km</a></li>\
    <li><a class="export" type_as="md">导出md</a></li>\
  </ul>\
</div>\
<div class="dropdown" id="new_group">\
  <button class="btn btn-default dropdown-toggle extends" type="button" id="new_btns" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">\
    新建与删除\
    <span class="caret"></span>\
  </button>\
  <ul class="dropdown-menu" aria-labelledby="new_btns" style="min-width:109px">\
    <li><a class="new" type_as="km">新建</a></li>\
    <li><a class="delete" type_as="mkm">删除</a></li>\
  </ul>\
</div>'])

    $("#save_group").css({"z-index":"999", "position":"fixed", "right": "1px"})
    $("#new_group").css({"z-index":"999", "position":"fixed", "right": "111px"})
    $("#homepage").css({"z-index":"999", "position":"fixed", "right": "221px", "height": "29px", "width": "100px", "font-size": "14px"})

    $("#homepage").click(function(){
        window.open("/")
    })

    var btn_km = $('.save')
    // btn_km.css({"position":"fixed", "right": "1px"})
    btn_km.click(function() {
        save_as("mkm")
    })

    var btn_md = $('.export[type_as="km"]')
    // btn_md.css({"position":"fixed", "right": "71px"})
    btn_md.click(function() {
        save_as("km")
    })

    var btn_md = $('.export[type_as="md"]')
    // btn_md.css({"position":"fixed", "right": "281px"})
    btn_md.click(function() {
        save_as("md")
    })

    var btn_del = $('.delete')
    // btn_del.css({"position":"fixed", "right": "141px"})
    btn_del.click(function() {
        if (true == confirm("慎重，是否执行删除操作？")){
            let url = decodeURIComponent(window.location.pathname)
            let rpath = get_url_rpath()
            $.post("/delete_kmfile/", {filename: rpath}, function(data, result){
                if (result == "success") {
                    let idx = url.lastIndexOf("/")
                    if (idx < 0) { idx = 0 }
                    let reopen_url = url.slice(0, idx)
                    // console.log(">>>", reopen_url, "\n<<<", rpath)
                    window.open(reopen_url, '_self')
                } else {
                    alert("[-] 操作失败！")
                }
            })
        }
    })

    var btn_new = $('.new')
    // btn_new.css({"position":"fixed", "right": "211px"})
    btn_new.click(function() {
        let uuid = new Date().getTime()
        let name = prompt("请输入新建脑图文件的名称:", uuid)
        if (name!=null && name!="") {
            $.post("/new_kmfile/", {filename: name}, function(data, result){
                if (result == "success") {
                    window.open("/listdir/" + name + ".mkm");
                } else {
                    alert("[-] 操作失败！")
                }
            })
        }
    })
})();
